<?php
/**
 * 模块配置控制器
 */

declare (strict_types = 1);

namespace app\controller\home;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;

class Dataauth extends BaseController
{
    /**
     * 获取模块配置列表（向后兼容方法）
     */
    public function index()
    {
        return $this->list();
    }
    
    /**
     * 获取模块配置列表
     */
    public function list()
    {
        try {
            // 查询模块配置列表
            $list = Db::name('data_auth')
                ->order('id', 'desc')
                ->select()
                ->toArray();
            
            // 格式化时间
            foreach ($list as &$item) {
                $item['create_time_format'] = $item['create_time'] ? date('Y-m-d H:i:s', $item['create_time']) : '';
                $item['update_time_format'] = $item['update_time'] ? date('Y-m-d H:i:s', $item['update_time']) : '';
            }
            
            return json([
                'code' => 0,
                'msg' => 'success',
                'data' => $list
            ]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '查询失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 查看模块配置详情
     */
    public function view()
    {
        try {
            $param = Request::param();
            
            if (empty($param['id'])) {
                return json(['code' => 400, 'msg' => '参数错误']);
            }
            
            // 查询模块配置详情
            $dataAuth = Db::name('data_auth')
                ->where('id', $param['id'])
                ->find();
            
            if (empty($dataAuth)) {
                return json(['code' => 404, 'msg' => '模块配置不存在']);
            }
            
            $dataAuth['create_time_format'] = $dataAuth['create_time'] ? date('Y-m-d H:i:s', $dataAuth['create_time']) : '';
            $dataAuth['update_time_format'] = $dataAuth['update_time'] ? date('Y-m-d H:i:s', $dataAuth['update_time']) : '';
            
            // 处理用户ID数组
            if (!empty($dataAuth['uids'])) {
                $uids = explode(',', trim($dataAuth['uids'], ','));
                $dataAuth['uids_array'] = $uids;
                $dataAuth['uids_count'] = count($uids);
                
                // 获取用户信息
                $users = Db::name('admin')
                    ->where('id', 'in', $uids)
                    ->column('name', 'id');
                $dataAuth['users'] = $users;
            }
            
            return json([
                'code' => 0,
                'msg' => 'success',
                'data' => $dataAuth
            ]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '查询失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 编辑模块配置
     */
    public function edit()
    {
        $param = Request::param();
        
        // 数据验证
        if (empty($param['id'])) {
            return json(['code' => 400, 'msg' => '参数错误']);
        }
        
        $data = [
            'id' => $param['id'],
            'title' => $param['title'] ?? '',
            'name' => $param['name'] ?? '',
            'desc' => $param['desc'] ?? '',
            'module' => $param['module'] ?? '',
            'uids' => isset($param['uids']) ? ',' . implode(',', $param['uids']) . ',' : '',
            'conf_1' => $param['conf_1'] ?? '',
            'conf_2' => $param['conf_2'] ?? '',
            'conf_3' => $param['conf_3'] ?? '',
            'conf_4' => $param['conf_4'] ?? '',
            'conf_5' => $param['conf_5'] ?? '',
            'conf_6' => $param['conf_6'] ?? '',
            'conf_7' => $param['conf_7'] ?? '',
            'conf_8' => $param['conf_8'] ?? '',
            'conf_9' => $param['conf_9'] ?? '',
            'conf_10' => $param['conf_10'] ?? '',
            'update_time' => time()
        ];
        
        try {
            Db::name('data_auth')->update($data);
            
            return json(['code' => 0, 'msg' => '保存成功']);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '保存失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 新建模块配置
     */
    public function add()
    {
        $param = Request::param();
        
        // 数据验证
        if (empty($param['title'])) {
            return json(['code' => 400, 'msg' => '权限名称不能为空']);
        }
        
        if (empty($param['name'])) {
            return json(['code' => 400, 'msg' => '权限标识不能为空']);
        }
        
        if (empty($param['module'])) {
            return json(['code' => 400, 'msg' => '所属模块不能为空']);
        }
        
        // 检查权限标识是否已存在
        $exist = Db::name('data_auth')
            ->where('name', $param['name'])
            ->find();
        
        if ($exist) {
            return json(['code' => 400, 'msg' => '权限标识已存在']);
        }
        
        $data = [
            'title' => $param['title'],
            'name' => $param['name'],
            'desc' => $param['desc'] ?? '',
            'module' => $param['module'],
            'uids' => isset($param['uids']) ? ',' . implode(',', $param['uids']) . ',' : '',
            'conf_1' => $param['conf_1'] ?? '',
            'conf_2' => $param['conf_2'] ?? '',
            'conf_3' => $param['conf_3'] ?? '',
            'conf_4' => $param['conf_4'] ?? '',
            'conf_5' => $param['conf_5'] ?? '',
            'conf_6' => $param['conf_6'] ?? '',
            'conf_7' => $param['conf_7'] ?? '',
            'conf_8' => $param['conf_8'] ?? '',
            'conf_9' => $param['conf_9'] ?? '',
            'conf_10' => $param['conf_10'] ?? '',
            'create_time' => time(),
            'update_time' => time()
        ];
        
        try {
            $id = Db::name('data_auth')->insertGetId($data);
            
            return json(['code' => 0, 'msg' => '保存成功', 'data' => ['id' => $id]]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '保存失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 删除模块配置
     */
    public function del()
    {
        $param = Request::param();
        
        // 数据验证
        if (empty($param['id'])) {
            return json(['code' => 400, 'msg' => '参数错误']);
        }
        
        // 检查模块配置是否存在
        $dataAuth = Db::name('data_auth')
            ->where('id', $param['id'])
            ->find();
        
        if (empty($dataAuth)) {
            return json(['code' => 404, 'msg' => '模块配置不存在']);
        }
        
        try {
            // 删除模块配置
            Db::name('data_auth')
                ->where('id', $param['id'])
                ->delete();
            
            return json(['code' => 0, 'msg' => '删除成功']);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '删除失败: ' . $e->getMessage()]);
        }
    }
}